import tool
from bs4 import BeautifulSoup
import single
import multiple
import threading
import log
class Pics:
    pics = []
    single = []
    multiple = []
    manga = []
    ugoku = []
    def __init__(self, url = '', dir = '', tag = '', opener = None):
        self.url = url
        print self.url
        self.tag = tag
        self.opener = opener
        self.dir = dir
        self.tool = tool.Tool()
        self.classify()
        self.begin()

    def classify(self):
        bs = BeautifulSoup(self.opener.open(self.url), "html.parser")



        self.pics = self.tool.Pic_Style_Json(str(bs))

        self.single = self.pics[0]
        self.multiple = self.pics[1]
        self.manga = self.pics[2]
        self.ugoku = self.pics[3]

    def begin(self):
        threads = []
        ll = log.Log()
        for i in range(0,len(self.single)):
            t = threading.Thread(target=single.Single,args=(self.single[i],self.dir, self.tag, self.opener))
            threads.append(t)
            #single.Single(url = self.single[i], dir = self.dir, opener = self.opener)
        for j in range(0,len(self.multiple)):
            t = threading.Thread(target=multiple.Multiple, args=(self.multiple[j], self.dir, self.tag, self.opener))
            threads.append(t)
            #multiple.Multiple(url = self.multiple[j], dir = self.dir, opener = self.opener)
        for k in range(0,len(self.manga)):
            t = threading.Thread(target=multiple.Multiple, args=(self.manga[k], self.dir, self.tag, self.opener))
            threads.append(t)
        for l in range(0,len(self.ugoku)):
            ll.ugoku_log(self.tag,self.ugoku[l])
            #multiple.Multiple(url=self.manga[k], dir=self.dir, opener=self.opener)
        if len(threads) > 0:
            for t in threads:
                t.setDaemon(False)
                t.start()
            t.join()
        else:
            print "This page don't have pictures!"